#include "loginform.h"

#include "loginform/registerform.h"
#include "ui_loginform.h"

#include <Application.h>
#include <CommonUtils.h>
#include <QFile>
#include <QLabel>
#include <QMessageBox>
#include <QPixmap>
#include <data/UserInfo.pb.h>
#include <mainwindow/mainwindow.h>
#include <method/Method.pb.h>
#include <method/Request.pb.h>
#include <method/Response.pb.h>

using namespace proto;

LoginForm::LoginForm(QWidget* parent)
    : BasicWindows(parent), ui(new Ui::LoginForm) {
    ui->setupUi(this);
    
    // 过滤下拉框点击事件
    ui->comboBox_account->installEventFilter(this);

    loadStyleSheet("loginform");

    titleBar = new TitleBar(this);
    titleBar->setButtonType(ButtonType::MIN_BUTTON);
    titleBar->setTitleWidth(width() - 20);
    titleBar->move(10, 10);
    titleBar->enableMove(true);

    initTitleBar(titleBar);
    initConnections();

    initList();

    ui->btn_login->setText(STRING_DISCONNECTED);

    connect(Application::instance(), &Application::signalConnected, [this]() { onTcpConnected(true); });
    connect(Application::instance(), &Application::signalDiconnected, [this]() { onTcpConnected(false); });
    connect(this, &LoginForm::signalTryConncet, Application::instance(), &Application::onTryConnect);
    connect(ui->btn_register, &QPushButton::clicked, this, &LoginForm::onBtnRegisterClicked);
    connect(ui->comboBox_account, &QComboBox::currentTextChanged, this, &LoginForm::onComboBoxAccountTextChange);
    connect(ui->edit_password, &QLineEdit::textChanged, [this](const QString& password) { this->loginAccount.password = password; });
}

LoginForm::~LoginForm() { delete ui; }

bool LoginForm::eventFilter(QObject* obj, QEvent* event) {
    // 过滤下拉框点击事件
    if (obj == ui->comboBox_account) {
        if (event->type() == QEvent::MouseMove) {
            return true;
        }
    }
    return QDialog::eventFilter(obj, event);
}

void LoginForm::initConnections() {
    connect(this->ui->btn_login, &QPushButton::clicked, this,
        &LoginForm::onBtnLoginClicked);
    connect(Application::instance(), &Application::signalLogin, this,
        &LoginForm::onLogin);
}

void LoginForm::onBtnLoginClicked(bool checked) {
    if (isConnected) {
        std::string account  = loginAccount.account.toStdString();
        std::string password = loginAccount.password.toStdString();

        // account.assign(ui->edit_account->text().toStdString());
        // account.assign(ui->comboBox_account->currentText().toStdString());
        // password.assign(ui->edit_password->text().toStdString());

        Application::instance()->Login(account, password);
    }
    else {
        ui->btn_login->setText(STRING_DISCONNECTED);
        emit signalTryConncet();
    }
}

void LoginForm::onBtnRegisterClicked(bool) {
    RegisterForm form;
    this->hide();
    form.exec();

    auto& account = form.getAccount();
    if (account.account() != "") {
        // ui->edit_account->setText(account.account().c_str());
        // ui->edit_password->setText(account.password().c_str());
        ui->comboBox_account->addItem(QString::fromStdString(account.account()));
        ui->comboBox_account->setCurrentText(QString::fromStdString(account.account()));
        ui->edit_password->clear();
        ui->btn_login->setFocus();
    }
    this->show();
}

void LoginForm::success() {
    this->close();
    SQLiteUtils utils;
    utils.pushAccount(loginAccount);
}

void LoginForm::initList() {
    SQLiteUtils utils;
    list = utils.getLoginAccounts();
    if (!list.isEmpty()) {
        auto a = list[0];
        ui->edit_password->setText(a.password);
        ui->label_head->setHeadshot(a.headshot);
        loginAccount.account  = a.account;
        loginAccount.password = a.password;

        for (auto&& i : list) {
            ui->comboBox_account->addItem(i.account);
        }
    }
}

void LoginForm::onLogin(int code, QString message) {
    qDebug() << "登录: " << message;

    if (!code) {
        const data::UserInfo& userInfo = Application::Myself();
        loginAccount.account           = userInfo.account().c_str();
        loginAccount.userId            = userInfo.userid().c_str();
        loginAccount.headshot          = userInfo.headshot().c_str();

        success();
    }
    else {
        QMessageBox::warning(this, "通知", message);
    }
}

void LoginForm::onComboBoxAccountTextChange(const QString& arg) {
    auto account = arg;

    loginAccount.account = account;

    for (auto&& i : list) {
        if (i.account == account) {
            loginAccount.password = i.password;
            ui->edit_password->setText(i.password);
            ui->label_head->setHeadshot(i.headshot);
            return;
        }
    }
}

void LoginForm::onTcpConnected(bool isConnected) {
    this->isConnected = isConnected;
    if (isConnected) {
        ui->btn_login->setText(STRING_CONNECTED);
    }
    else {
        ui->btn_login->setText("尝试连接服务器...");
    }
}
